﻿using System;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;

public partial class Admin_ReportSendInvoice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string companyCode = Request.QueryString["CompanyCode"];
        if (!string.IsNullOrEmpty(companyCode))
        {
            string isSend = Request.QueryString["Send"];
            var company = DataRepository.AbAccountCodeProvider.GetByKindPment(companyCode);
            var prepareToSend = (VList<AllInvoice>)Session[companyCode];
            var companyDs = new Send.CompanyDSDataTable();
            var companyRow = companyDs.NewCompanyDSRow();
            companyRow.CompanyName = company.AccName;
            companyRow.Address = company.CompanyAddress;
            companyRow.Attn = company.Attention;
            companyDs.AddCompanyDSRow(companyRow);
            ReportViewer1.LocalReport.DataSources["Send_CompanyDS"].Value = companyDs;

            var invoiceDs = new Send.InvoiceDSDataTable();
            foreach (var invoice in prepareToSend)
            {
                var newinvoiceRow = invoiceDs.NewInvoiceDSRow();
                newinvoiceRow.Reference = invoice.VatInvoiceNumber;
                newinvoiceRow.DateVisit = invoice.DateVisit;
                newinvoiceRow.Description = invoice.PatientName;
                if (invoice.TotalAmountUsd != null) newinvoiceRow._Amount_USD_ = invoice.TotalAmountUsd.Value;
                if (invoice.TotalAmountVnd != null) newinvoiceRow._Amount_VND_ = invoice.TotalAmountVnd.Value;
                invoiceDs.AddInvoiceDSRow(newinvoiceRow);
                if (isSend == "True")
                {
                    var dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(invoice.VatInvoiceNumber, invoice.VatInvoiceNumber.Substring(5, 5));
                    dailyTrans.SendDate = DateTime.Now;
                    DataRepository.DailyTransProvider.Update(dailyTrans);
                }
            }
            ReportViewer1.LocalReport.DataSources["Send_InvoiceDS"].Value = invoiceDs;
            if (isSend == "True")
                Session[companyCode] = null;
            ReportViewer1.DataBind();
        }
    }

}
